package com.ruoyi.service.impl;

import com.github.pagehelper.PageInfo;
import com.hundun.vision.api.common.PlainResult;
import com.hundun.vision.api.dto.FeatureDTO;
import com.hundun.vision.api.dto.OperatorDTO;
import com.hundun.vision.api.dto.StrategyDTO;
import com.hundun.vision.api.query.StrategyQuery;
import com.hundun.vision.api.request.StrategyRequest;
import com.hundun.vision.api.service.RPCStrategyService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ResultUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.service.FeatureService;
import com.ruoyi.service.OperatorService;
import com.ruoyi.service.StrategyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ：RuoChen
 * @date ：17:29 2021/6/11
 * @description：策略服务实现类
 */
@Slf4j
@Component
public class StrategyServiceImpl implements StrategyService {

    @DubboReference(timeout = 60000, retries = 0)
    private RPCStrategyService strategyService;

    @Autowired
    private FeatureService featureService;

    @Autowired
    private OperatorService operatorService;

    @Override
    public Boolean save(StrategyRequest request) {
        log.info("save strategy,request:{}", request);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        request.setUserId(user.getUserId());
        PlainResult<Boolean> result = strategyService.save(request);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return false;
    }

    @Override
    public Integer removeByIds(List<Long> strategyIds) {
        log.info("remove strategy by ids:{}", strategyIds);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<Integer> result = strategyService.removeByIds(user.getUserId(), strategyIds);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return 0;
    }

    @Override
    public Boolean update(StrategyRequest request) {
        log.info("update strategy,request:{}", request);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        request.setUserId(user.getUserId());
        PlainResult<Boolean> result = strategyService.update(request);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return false;
    }

    @Override
    public List<FeatureDTO> getFeatures() {
        log.info("get features");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        return featureService.getByUserId(user.getUserId());
    }

    @Override
    public List<OperatorDTO> getOperators() {
        log.info("get operators");
        return operatorService.list();
    }

    @Override
    public StrategyDTO getById(Long strategyId) {
        log.info("get strategy by id,strategyId:{}", strategyId);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<StrategyDTO> result = strategyService.getById(user.getUserId(), strategyId);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }

    @Override
    public PageInfo<StrategyDTO> search(StrategyQuery query) {
        log.info("search strategy,query:{}", query);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        query.setUserId(user.getUserId());
        PlainResult<PageInfo<StrategyDTO>> result = strategyService.search(query);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }
}
